/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.util.*;

abstract class App {

  private File spec;
  protected final File getSpec() {return spec;}

  private boolean verbose;
  protected final void setVerbose(boolean verbose) {this.verbose = verbose;}
  protected final boolean isVerbose() {return verbose;}

  App(boolean verbose) {
    this.verbose = verbose;
  }

  App() {
    this(false);
  }

  protected final void note(Object msg) {
    note(msg,false);
  }
  protected final void note(Object msg,boolean force) {
    if (force || isVerbose()) System.err.println("[" + getClass().getName() + "] " + msg);
  }

  abstract void go() throws Exception;
  

  /**
   * @param args options, if empty we use {"spec.txt"}
   */
  public final void realMain(String[] args) throws Exception {

    // Defaults
    if (args == null || args.length == 0) {
      args = new String[]{"spec.txt"};
    }
    
    // Get the specFile
    String specName = null;
    for (int i=0; i<args.length;) {
      String arg = args[i++];
      if (arg.startsWith("-")) {
        if ("-v".equals(arg) || "-verbose".equals(arg)) {
          setVerbose(true);
        }
      } else {
        specName = arg;
      }
    }
    
    if (specName == null) {
      throw new IllegalArgumentException("need spec file");
    }
    spec = new File(specName);
    readOptions(args);
    go();
  }

  /** @override */
  void readOptions(String[] args) {}
}